package com.gxa.xiaoucomeshome.controller;

import com.gxa.xiaoucomeshome.pojo.Manager;
import com.gxa.xiaoucomeshome.pojo.OrderLog;
import com.gxa.xiaoucomeshome.service.ManagerService;
import com.gxa.xiaoucomeshome.utils.JsonResult;
import com.gxa.xiaoucomeshome.utils.PasswordUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: Mr.Wang
 * @create: 2022-06-07 19:27
 **/
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@RequestMapping("/mgr")
@RestController
@Api(tags = "站点后台接口API")
@CrossOrigin
@ControllerAdvice
public class ManagerController {

    @Autowired
    ManagerService managerService;
    @Autowired
    PasswordUtils passwordUtils;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "manager",value = "管理员手机号,密码",paramType = "body",dataType = "Manager",required = true)
    })
    @PostMapping(value = "/login")
    @ApiOperation(value = "管理员登录",produces = "body")
    public Map<String,Object> login(@RequestBody Manager manager, @ApiIgnore HttpSession session) throws Exception {
        //密码加密
        String managerPwd = manager.getPwd();
        String passwordUtils = this.passwordUtils.passwordUtils(managerPwd);
        managerService.login(manager.getPhone(), passwordUtils);
        //登录成功,返回token值
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //token存到服务器
        session.setAttribute("token",token);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("登录成功");
        jsonResult.set("token",token);
        return jsonResult.getMap();
    }
    @PostMapping(value = "/index")
    @ApiOperation(value = "站点后台首页",produces = "application/json")
    public Map<String ,Object> mgrIndex(){
        Integer checkOrders = managerService.checkOrders();
        Integer countOrders = managerService.countOrders();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("管理员首页");
        //未审核订单数
        jsonResult.set("checkOrders",checkOrders);
        //待分配快递员订单数
        jsonResult.set("countOrders", countOrders);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "管理员修改密码",produces = "body")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号",paramType = "String",required = true),
            @ApiImplicitParam(name = "pwd",value = "原密码",paramType = "String",required = true),
            @ApiImplicitParam(name = "newPwd",value = "新密码",paramType = "String",required = true)
    })
    @PostMapping("/update")
    public Map<String,Object> updatePwd(String phone, String pwd, String newPwd) throws Exception {
        pwd = passwordUtils.passwordUtils(pwd);
        newPwd = passwordUtils.passwordUtils(newPwd);
        managerService.upadate(newPwd, phone, pwd);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }
    @ApiOperation(value = "增加物料库存",produces = "body")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialNumber",value = "增加数量",paramType = "Integer",required = true),
            @ApiImplicitParam(name = "materialPrice",value = "物料单价",paramType = "Double",required = true),
            @ApiImplicitParam(name = "materialName",value = "物料名称",paramType = "String",required = true),
    })
    @PostMapping("/addMaterial")
    public Map<String,Object> addMaterial(Integer materialNumber, Double materialPrice, String materialName) {
        managerService.addMaterial(materialNumber, materialPrice, materialName);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("增加成功");
        return jsonResult.getMap();
    }
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数",name = "page",defaultValue = "1")
    })
    @GetMapping("/dispatchList")
    @ApiOperation(value = "待派送订单列表",produces = "query")
    public Map<String,Object> selectDispatchList(Integer page){
        Map<String, Object> selectDispatchList = managerService.selectDispatchList(page);
        Map<String, Object> list = managerService.selectDispatchList(null);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("待派送列表");
        jsonResult.setData(selectDispatchList);
        jsonResult.set("total",list.size());
        return jsonResult.getMap();
    }
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数",name = "page",defaultValue = "1"),
            @ApiImplicitParam(value = "查询关键字",name = "str"),
            @ApiImplicitParam(value = "审核开始时间",name = "from"),
            @ApiImplicitParam(value = "审核截止时间",name = "to"),
    })
    @GetMapping("/likeDispatchList")
    @ApiOperation(value = "待派送列表模糊查询",produces = "params")
    public Map<String,Object> selectLikeDispatchList(Integer page,String str,String from,String to){
        Map<String, Object> selectLikeDispatchList = managerService.selectLikeDispatchList(page,str, from, to);
        Map<String, Object> list = managerService.selectLikeDispatchList(null, str, from, to);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询结果");
        jsonResult.setData(selectLikeDispatchList);
        jsonResult.set("total",list.size());
        return jsonResult.getMap();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "模糊查询快递员名字或手机号",name = "courier")
    })
    @GetMapping("/selectCourierList")
    @ApiOperation(value = "快递员详情列表",produces = "params")
    public Map<String,Object> selectCourierList(String courier){
        Map<String ,Object> selectCourierList = managerService.selectCourierList(courier);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("快递员列表");
        jsonResult.setData(selectCourierList);
        jsonResult.set("total",selectCourierList.size());
        return jsonResult.getMap();
    }
    @ApiOperation(value = "快递员订单分配")
    @PostMapping("/chooseCourier")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "快递员手机号",name = "courierPhone",required = true),
    })
    public Map<String,Object> chooseCourier(String courierPhone, String[] logids){
        for (String logid : logids) {
            managerService.chooseCourier(courierPhone,logid);
            managerService.setState(logid);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("快递员选择成功");
        return jsonResult.getMap();
    }

    @ApiOperation("待审核列表")
    @GetMapping("/selectCheckList")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数",name = "page",defaultValue = "1"),
            @ApiImplicitParam(name = "size",value = "每页数量", defaultValue = "10")
    })
    public Map<String, Object> selectCheckList(Integer page, Integer size){
        List<Map<String, Object>> maps = managerService.selectCheckList((page-1)*10, size);
        List<Map<String, Object>> mapList = managerService.selectCheckList(null, null);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("1");
        jsonResult.setData(maps);
        jsonResult.set("total",mapList.size());
        return jsonResult.getMap();
    }

    @ApiOperation(value = "添加收银条台号",produces = "body")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单编号",paramType = "String",required = true),
            @ApiImplicitParam(name = "number",value = "收银台条号",paramType = "String",required = true),
            @ApiImplicitParam(name = "bacth",value = "流水号",paramType = "String",required = true),
            @ApiImplicitParam(name = "date",value = "日期",paramType = "String",required = true),
    })
    @PostMapping("/addBatch")
    public Map<String, Object> addBarcode(String orderId, String number, String bacth, String date) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date parseDate = df.parse(date);
        managerService.addBarcode(orderId, number, bacth, parseDate);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加收银条信息成功");
        return jsonResult.getMap();
    }
    @ApiOperation(value = "添加物流详情方法",produces = "body")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "OrderLog",value = "物流对象",paramType = "OrderLog",dataType = "body")
    })
    @PostMapping(value = "/addOrderLog")
    public Map<String, Object> addOrderLog(OrderLog orderLog){
        orderLog.setDeliveryTime(new Date(System.currentTimeMillis()));
        Integer integer = managerService.addOrderLog(orderLog);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加订单物流信息成功");
        jsonResult.setData(integer);
        return jsonResult.getMap();
    }

    @ApiOperation("物料名称列表")
    @PostMapping("/selectAllMaterialsName")
    public Map<String, Object> selectAllMaterialsName(){
        String[] s = managerService.selectAllMaterialsName();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("物料名称");
        jsonResult.setData(s);
        return jsonResult.getMap();
    }
    @ApiOperation("物料库存列表")
    @PostMapping("/selectAllMaterials")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数",name = "page",defaultValue = "1"),
            @ApiImplicitParam(name = "size",value = "每页数量", defaultValue = "10")
    })
    public Map<String, Object> selectAllMaterials(Integer page, Integer size){
        Integer offset = (page - 1) * size;
        List<Map<String, Object>> maps = managerService.selectAllMaterials(offset, size);
        List<Map<String, Object>> mapList = managerService.selectAllMaterials(null, null);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("物料库存查询成功");
        jsonResult.setData(maps);
        jsonResult.set("total",mapList.size());
        return jsonResult.getMap();
    }
    @ApiOperation("模糊查询物料库存列表")
    @PostMapping("/searchMaterials")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数",name = "page",defaultValue = "1"),
            @ApiImplicitParam(name = "size",value = "每页数量", defaultValue = "10"),
            @ApiImplicitParam(name = "materialName",value = "材料名称")
    })
    public Map<String, Object> searchMaterials(String materialName, Integer page, Integer size){
        Integer offset = (page - 1) * size;
        List<Map<String, Object>> maps = managerService.searchMaterials(materialName, offset, size);
        List<Map<String, Object>> mapList = managerService.searchMaterials(materialName, null,null);
        int size1 = mapList.size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("物料库存查询成功");
        jsonResult.setData(maps);
        jsonResult.set("total",size1);
        return jsonResult.getMap();
    }
    @ApiOperation("根据物料名称删除物料")
    @PostMapping("/deleteMaterial")
    public Map<String, Object> deleteMaterial(String[] materialName) {
        managerService.deleteMaterial( materialName);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }
    @ApiOperation(value = "平台派单订单号")
    @PostMapping("/platformDispatch")
    public Map<String,Object> platformDispatch(String[] orderIds){
        for (String orderId : orderIds) {
            //设置配送员可抢单
            managerService.platformDispatch(orderId);
            //设置抢单时间
            managerService.setDeliveryTime(orderId);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("平台派单成功");
        return jsonResult.getMap();
    }
    @ApiOperation(value = "获取派送列表详情",produces = "params")
    @GetMapping(value = "/deliveryDetails")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId",value = "订单编号",required = true)})
    public Map<String,Object> deliveryDetails(String orderId){
        Map<String, Object> deliveryDetails = managerService.deliveryDetails(orderId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("派单详情获取成功");
        jsonResult.setData(deliveryDetails);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "配送列表/更改+模糊查询",produces = "params")
    @GetMapping("/setShippingList")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数",name = "page",defaultValue = "1"),
            @ApiImplicitParam(value = "查询关键字",name = "keywords"),
            @ApiImplicitParam(value = "派单时间:开始",name = "from"),
            @ApiImplicitParam(value = "派单时间:截止",name = "to"),
            @ApiImplicitParam(value = "订单状态",name = "state"),
            @ApiImplicitParam(value = "签收情况",name = "receipt"),
            @ApiImplicitParam(value = "时效产品",name = "agingProduct")
    })
    public Map<String,Object> setShippingList(Integer page,String keywords,String from,String to,Integer state,
                                              String receipt,String agingProduct){
        Integer offset = (page-1)*10;
        Map<String, Object> setShippingList = managerService.setShippingList(offset,keywords,from,to,state,receipt,agingProduct);
        Map<String, Object> map = managerService.setShippingList(null, keywords,from,to,state,receipt,agingProduct);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("配送列表/更改");
        jsonResult.setData(setShippingList);
        jsonResult.set("total",map.size());
        return jsonResult.getMap();
    }
    @GetMapping("/setRemark")
    @ApiOperation(value = "订单备注",produces = "params")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "备注信息",name = "remark",required = true),
            @ApiImplicitParam(value = "订单编号",name = "orderId",required = true)
    })
    public Map<String,Object> setRemark(String remark,String orderId){
        managerService.setRemark(remark, orderId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("订单备注");
        return jsonResult.getMap();
    }
    @ApiOperation(value = "订单记录删除",produces = "params")
    @GetMapping("/deleteOrderId")
    public Map<String,Object> deleteOrderId(String[] orderIds){
        for (String orderId : orderIds) {
            managerService.deleteOrderId(orderId);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("订单记录删除");
        return jsonResult.getMap();
    }
    @ApiOperation(value = "配送列表/更改-订单详情",produces = "params")
    @GetMapping("/selectDetails")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "订单编号",name = "orderId",required = true)
    })
    public Map<String,Object> selectDetails(String orderId){
        Map<String, String > selectDetails = managerService.selectDetails(orderId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("订单详情");
        jsonResult.setData(selectDetails);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "登出接口")
    @PostMapping("/loginOut")
    public Map<String,Object> loginOut(@ApiIgnore HttpSession session){
        //清除token
        session.removeAttribute("token");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("登出成功");
        return jsonResult.getMap();
    }

    @ApiOperation("待审核订单列表根据订单号删除订单")
    @PostMapping("/deleteCheckList")
    public Map<String,Object> deleteCheckList(String[] orderIds) {
        managerService.deleteCheckList(orderIds);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }
    @ApiOperation("模糊查询+待审核列表")
    @PostMapping("/serachLikeCheckList")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数",name = "page",defaultValue = "1"),
            @ApiImplicitParam(value = "关键字",name = "str"),
            @ApiImplicitParam(value = "状态",name = "state"),
            @ApiImplicitParam(value = "开始时间",name = "from"),
            @ApiImplicitParam(value = "结束时间",name = "to")
    })
    public Map<String,Object> serachLikeCheckList(
            Integer page, String str, Integer state, String from, String to){
        page = (page-1)*10;
        List<Map<String, Object>> maps = managerService.serachLikeCheckList(page, str, state, from, to);
        List<Map<String, Object>> listLength = managerService.serachLikeCheckList(null, str, state, from, to);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("1");
        jsonResult.setData(maps);
        jsonResult.set("total",listLength.size());
        return jsonResult.getMap();
    }
    @ApiOperation("订单状态改变")
    @PostMapping("/stateChange")
    public Map<String,Object> stateChange(Integer state, String orderId) {
        managerService.stateChange(state,orderId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("订单状态改变");
        return jsonResult.getMap();
    }
    @ApiOperation("添加物流订单物料消耗")
    @PostMapping("/addMaterialUse")
    public Map<String,Object> addMaterialUse(String logId, String name, String type, Integer num) {
        managerService.addMaterialUse(logId, name, type, num);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加物流订单物料消耗成功");
        return jsonResult.getMap();
    }
}
